package com.example.game;

import android.app.FragmentTransaction;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;


import java.util.Timer;
import java.util.TimerTask;

import android.app.Fragment;

import static com.example.game.GamePage.player;
import static com.example.game.GamePage.back_from_video_page;
import static com.example.game.GamePage.databaseHelper;
import static com.example.game.GamePage.player;

public class setVideoFragment extends Fragment
{
    private VideoView videoView;
    private setToolFragment ToolFragment;
    Button bt;
    private boolean video_is_finished = false;
    int time_count;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        time_count = 25;

        final View view = inflater.inflate(R.layout.video_play, container, false);
        player.flag_open_video_page = true;
//        GamePage.getMyTimer().cancel();
        video_is_finished = false;
        back_from_video_page = true;
        bt = (Button) view.findViewById(R.id.button_back_to_menu);
        videoView = (VideoView) view.findViewById(R.id.Video_Play);
        videoView.requestFocus();
        String videoUrl = "android.resource://" + "com.example.game" + "/raw/" + R.raw.video;
        videoView.setVideoPath(videoUrl);
        videoView.setMediaController(new MediaController(getActivity()));
        videoView.setVideoURI(Uri.parse(videoUrl));
        videoView.requestFocus();
        videoView.start();
        if (bt == null){
            Log.d("TAG", "button not found ");
        }else {
            Log.d("TAG", "button found");
        }
        bt.setOnClickListener((new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GamePage act = (GamePage) getActivity();
                FragmentTransaction transaction = act.fm.beginTransaction();
                ToolFragment = new setToolFragment();
                transaction.replace(R.id.Fragment, ToolFragment);
                transaction.commit();
                Log.d("article", "onClick: checkpoint");
            }
        }));
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Log.d("timer_TAG", "run: video_back_buttom");
                Log.d("TAG_video_bonus", "run: ");
                time_count -= 1;
                if (time_count == 0) {
                    bt.setText("可领取奖励,你拥有的每个传播途径都会提高你点击发布按钮的产量");
                    player.building_list[0][11] = 1;
                    video_is_finished = true;
                    timer.cancel();
                }else{
                    bt.setText(String.format("还有%d秒可领取奖励",time_count));
                }
            }
        },0,1000);
        return view;
    }
}
